#! /usr/bin/python3
# coding=utf-8
# @time: 2025/10/16 11:14
# @Author:鹤川
import pytest

from codings.doYunSuan import DoYunSuan
from common.doFile import DoFile
from common.doFile import myLogger

# 读取测试数据
df = DoFile()
test_data1 = df.doExcelReadDIV("jiaYunSuan.xlsx", "Sheet1", 2, 15, 2, 5)
test_title1 = df.doExcelReadDIV("jiaYunSuan.xlsx", "Sheet1", 2, 15, 1, 1)

test_data2 = df.doExcelReadDIV("jiaYunSuan.xlsx", "Sheet2", 2, 18, 2, 5)
test_title2 = df.doExcelReadDIV("jiaYunSuan.xlsx", "Sheet2", 2, 18, 1, 1)

test_data3 = df.doExcelReadDIV("jiaYunSuan.xlsx", "Sheet3", 2, 19, 2, 5)
test_title3 = df.doExcelReadDIV("jiaYunSuan.xlsx", "Sheet3", 2, 19, 1, 1)

test_data4 = df.doExcelReadDIV("jiaYunSuan.xlsx", "Sheet4", 2, 20, 2, 5)
test_title4 = df.doExcelReadDIV("jiaYunSuan.xlsx", "Sheet4", 2, 20, 1, 1)

test_title_str1 = [str(title) if not isinstance(title, str) else title for title in test_title1]
test_title_str2 = [str(title) if not isinstance(title, str) else title for title in test_title2]
test_title_str3 = [str(title) if not isinstance(title, str) else title for title in test_title3]
test_title_str4 = [str(title) if not isinstance(title, str) else title for title in test_title4]


class Test_DoYunSuan():

    # 类级别夹具 - 整个测试类执行前后执行一次
    @pytest.fixture(scope="class", autouse=True)
    def class_setup_teardown(self):
        """类级别的前置和后置操作"""
        print("=" * 50)
        print("🚀 开始执行四则运算测试类")
        print("=" * 50)
        myLogger.info("=" * 40)
        myLogger.info("开始执行四则运算测试类")
        myLogger.info("=" * 40)
        yield
        print("=" * 50)
        print("✅ 四则运算测试类执行完成")
        print("=" * 50)
        myLogger.info("=" * 40)
        myLogger.info("四则运算测试类执行完成")
        myLogger.info("=" * 40)

    # 方法级别夹具 - 每个测试方法执行前后执行
    @pytest.fixture(autouse=True)
    def method_setup_teardown(self):
        """方法级别的前置和后置操作"""
        # 前置操作 - 在每个测试方法前执行
        print("-" * 40)
        print("📝 开始执行测试方法")
        print("-" * 40)
        myLogger.info("-" * 30)
        myLogger.info("开始执行测试方法")
        self.ys = DoYunSuan()  # 为每个测试方法创建新的实例
        yield
        # 后置操作 - 在每个测试方法后执行
        print("-" * 40)
        print("🏁 测试方法执行完成")
        print("-" * 40)
        myLogger.info("测试方法执行完成")
        myLogger.info("-" * 30)

    # 原有测试方法保持不变，只添加了夹具功能
    @pytest.mark.parametrize("n1,n2,result,isOK", test_data1, ids=test_title_str1)
    def test_mySum_ok1(self, n1, n2, result, isOK):
        try:
            print(f"🧪 执行加法测试: {n1} + {n2} = {result}")
            myLogger.info("测试用例开始")
            # 使用夹具中已创建的实例，无需再次创建
            r = self.ys.mySum(n1, n2)
        except Exception as e:
            print(f"❌ 测试用例失败，原因：{e}")
            myLogger.exception(f"测试用例失败，原因：{e}")
        else:
            print(f"✅ 测试用例成功\n   期望结果：{result}\n   实际结果：{r}")
            myLogger.info("测试用例成功")
            assert isOK == "ok"

    @pytest.mark.parametrize("n1,n2,result,isOK", test_data2, ids=test_title_str2)
    def test_mySum_ok2(self, n1, n2, result, isOK):
        try:
            print(f"🧪 执行减法测试: {n1} - {n2} = {result}")
            myLogger.info("测试用例开始")
            # 使用夹具中已创建的实例
            r = self.ys.myJian(n1, n2)
        except Exception as e:
            print(f"❌ 测试用例失败，原因：{e}")
            myLogger.exception(f"测试用例失败，原因：{e}")
        else:
            print(f"✅ 测试用例成功\n   期望结果：{result}\n   实际结果：{r}")
            myLogger.info("测试用例成功")
            assert isOK == "ok"

    @pytest.mark.parametrize("n1,n2,result,isOK", test_data3, ids=test_title_str3)
    def test_mySum_ok3(self, n1, n2, result, isOK):
        try:
            print(f"🧪 执行乘法测试: {n1} × {n2} = {result}")
            myLogger.info("测试用例开始")
            # 使用夹具中已创建的实例
            r = self.ys.myCheng(n1, n2)
        except Exception as e:
            print(f"❌ 测试用例失败，原因：{e}")
            myLogger.exception(f"测试用例失败，原因：{e}")
        else:
            print(f"✅ 测试用例成功\n   期望结果：{result}\n   实际结果：{r}")
            myLogger.info("测试用例成功")
            assert isOK == "ok"

    @pytest.mark.parametrize("n1,n2,result,isOK", test_data4, ids=test_title_str4)
    def test_mySum_ok4(self, n1, n2, result, isOK):
        try:
            print(f"🧪 执行除法测试: {n1} ÷ {n2} = {result}")
            myLogger.info("测试用例开始")
            # 使用夹具中已创建的实例
            r = self.ys.myChu(n1, n2)
        except Exception as e:
            print(f"❌ 测试用例失败，原因：{e}")
            myLogger.exception(f"测试用例失败，原因：{e}")
        else:
            print(f"✅ 测试用例成功\n   期望结果：{result}\n   实际结果：{r}")
            myLogger.info("测试用例成功")
            assert isOK == "ok"
